package com.ctg.itrdc.imageconsole.data.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.data.dao.IUserImageShareDao;
import com.ctg.itrdc.imageconsole.data.dto.image.RepositoryDTO;
import com.ctg.itrdc.imageconsole.data.entity.UserImageShare;
import com.ctg.itrdc.paspsdk.common.entity.PageInfo;
import com.ctg.itrdc.paspsdk.data.dao.impl.BaseDaoImpl;

@Component("userImageShareDao")
public class UserImageShareDaoImpl extends BaseDaoImpl implements IUserImageShareDao {

	@Override
	public void deleteByTagIdAndRelateUserId(Long tagId, Long relateUserId) {
		String sql = "DELETE FROM USER_IMAGE_SHARE WHERE RELATE_USER_ID=? AND DOCKER_IMAGE_TAG_ID=?";
		this.getJdbcTemplate().update(sql, relateUserId, tagId);
	}

	@Override
	public PageInfo<RepositoryDTO> getImagesOfShare(Long currentUserId, Integer pageNum, Integer pageSize) {
		String sql = "SELECT distinct tag.image_name from docker_image_tag tag where tag.id in (SELECT imageshare.docker_image_tag_id from user_image_share imageshare where imageshare.user_id=?) ORDER BY tag.last_update_time DESC";
		List<Object> params = new ArrayList<Object>();
		params.add(currentUserId);

		return (PageInfo<RepositoryDTO>)this.jdbcFindPageInfo(sql, RepositoryDTO.class,
				params, pageNum, pageSize, true);
	}

	@Override
	public List<UserImageShare> getSharesByUserId(Long currentUserId) {
		UserImageShare imageShare = new UserImageShare();
		imageShare.setUserId(currentUserId);
		
		return super.selectByExample(imageShare);
	}

}
